package com.zimmur.app.mocolink.manage.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.form.TopicForm;
import com.zimmur.app.mocolink.domain.model.Topic;
import com.zimmur.app.mocolink.domain.model.TopicGoods;
import com.zimmur.app.mocolink.domain.param.TopicParamForManage;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.ITopicService;

@Controller
@RequestMapping("/topic")
public class TopicController extends BaseController{

	@Autowired
	private ITopicService topicService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request,ModelMap model){
		TopicParamForManage param = new TopicParamForManage();
		Integer pageNo=getParam("pageNo", 1);
		param.setPageNo(pageNo);
		PageUtil<Topic> page =  topicService.queryByParam(param);
		model.addAttribute("param", param);
		model.addAttribute("page", page);
		return "/center/topic/list";
	}
	@RequestMapping("/query")
	public String query(HttpServletRequest request,ModelMap model){
		TopicParamForManage param = new TopicParamForManage();
		Integer pageNo=getParam("pageNo", 1);
		Integer isChecked = getParam("isChecked", -99);
		String keyword = getParam("keyword");
		if(isChecked>=-1 && isChecked<=1){
			param.setIsChecked(isChecked);
		}
		if(!StringUtils.isBlank(keyword)){
			param.setKeyword(keyword);
		}
		param.setPageNo(pageNo);
		PageUtil<Topic> page =  topicService.queryByParam(param);
		model.addAttribute("param", param);
		model.addAttribute("page", page);
		return "/center/topic/list";
	}
	@RequestMapping("/add")
	public String add(HttpServletRequest request,ModelMap model){
		model.addAttribute("topic", new Topic());
		model.addAttribute("topicGoodsList", null);
		return "/center/topic/edit";
	}
	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,ModelMap model){
		Integer topicId = getParam("topicId", 0);
		Topic topic = topicService.selectById(topicId);
		List<TopicGoods> topicGoodsList = topicService.selectDetailByTopicId(topicId);
		model.addAttribute("topic", topic);
		model.addAttribute("topicGoodsList", topicGoodsList);
		return "/center/topic/edit";
	}
	@ResponseBody
	@RequestMapping("/delete")
	public ReturnJsonEntity delete(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer topicId = getParam("topicId", 0);
		int flag = topicService.deleteTopic(topicId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/save")
	public ReturnJsonEntity save(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getUserId();
		String username = getUsername();
		String json = getParam("topicJson");
		if(!StringUtils.isBlank(json)){
			TopicForm form = JSONObject.parseObject(json, TopicForm.class);
			form.setAdminId(userId);
			form.setAdminName(username);
			int flag = topicService.saveTopic(form);
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE000000;
			}
		}else{
			codeEnum = StatusCodeEnum.CODE400402;
		}
		
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping(value="/checkStatus" ,method = RequestMethod.POST)
	public ReturnJsonEntity checkStatus(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer topicId = getParam("topicId", 0);
		Integer status = getParam("status", 0);
		int flag = topicService.checkStatus(topicId, status);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum = StatusCodeEnum.CODE400401;
		}
		entity.init(codeEnum);
		return entity;
	}
}
